package br.com.lh.urabuilder.view.properties
{
	import br.com.lh.urabuilder.domain.xml.UraAction;
	import br.com.lh.urabuilder.domain.xml.UraActionField;
	
	import mx.binding.utils.BindingUtils;
	import mx.containers.FormItem;
	import mx.controls.ComboBox;
	import mx.controls.NumericStepper;
	import mx.controls.TextArea;
	import mx.controls.TextInput;
	import mx.core.UIComponent;
	import mx.validators.NumberValidator;
	
	/**
	 * Builds a UraFormItem based on the type and the name
	 * of a given action property,performing a binding 
	 * between the model and the view values.
	 * 
	 * @author Luiz Henrique
	 * 
	 */
	public class UraFormItem extends FormItem
	{
		public function UraFormItem(nodeType:String,nodeName:String,uraAction:UraAction){
				var uraActionField:UraActionField = uraAction[nodeName];
				label = nodeName;
				var input:UIComponent;
				if (uraActionField.possibleValues !=null){
					input= new ComboBox();
					ComboBox(input).dataProvider= uraActionField.possibleValues;
					ComboBox(input).selectedItem = uraActionField.value;
					BindingUtils.bindProperty(uraActionField,"value",input,"selectedItem");
				}else{
					required = uraActionField.validator.required;
					if (uraActionField.validator is NumberValidator){
						input= new NumericStepper();
						NumericStepper(input).value= Number(uraActionField.value);
						BindingUtils.bindProperty(uraActionField,"value",input,"value");
						uraActionField.validator.source = input;
						uraActionField.validator.property = "value";		
					}else{
						var value:String = String(uraActionField.value);
						if (value == "null"){
							value ="";
						}
						if (uraActionField.unlimitted){
							input = new TextArea();
							TextArea(input).text = value;
							input.width = 250;
							input.height= 150;
							BindingUtils.bindProperty(uraActionField,"value",input,"text");
							uraActionField.validator.source = input;
							uraActionField.validator.property = "text";
						}else{
							input= new TextInput();
							TextInput(input).text = value;
							BindingUtils.bindProperty(uraActionField,"value",input,"text");
							uraActionField.validator.source = input;
							uraActionField.validator.property = "text";
						}
					}	
				}
			addChild(input);
		}
	}
}